package netective.api.gatherer;

import netective.dao.DatabaseHelper;
import jpcap.packet.IPPacket;
import jpcap.packet.TCPPacket;
import jpcap.packet.ARPPacket;
import jpcap.packet.UDPPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.Packet;

/**
 *
 * @author marek
 */
public class PacketPrinter extends Thread {

	private Captor captor;
	private DatabaseHelper dh;
	
	public PacketPrinter(Captor captor)
	{
		this.captor = captor;
		dh=new DatabaseHelper(captor);
	}
	
	@Override
	public void run() {
		captor.setDeviceChanged(false);
		
        while (!captor.hasDeviceChanged()) 
        {
            Packet packet = captor.getCaptor().getPacket();
            if (packet != null) {
            	if (packet instanceof TCPPacket)
            	{
            		dh.savePacket((TCPPacket)packet);
            		System.out.println("Protocol TCP");
            		((TCPPacket)packet).data.toString();
            	}
            	else if (packet instanceof UDPPacket)
            	{
            		dh.savePacket((UDPPacket)packet);
            		System.out.println("Protocol UDP");
            		((UDPPacket)packet).data.toString();
            	}
            	else if (packet instanceof ICMPPacket)
            	{
            		dh.savePacket((ICMPPacket)packet);
            		System.out.println("Protocol ICMP");
            		((ICMPPacket)packet).data.toString();
            	}
            	else if (packet instanceof ARPPacket)
            	{
            		dh.savePacket((ARPPacket)packet);
            		System.out.println("Protocol ARP");
            		((ARPPacket)packet).data.toString();
            	}
            	else if (packet instanceof IPPacket)
            	{
            		dh.savePacket((IPPacket)packet);
            		System.out.println("Protocol IP");
            	}
            	else
            		System.out.println("Protocol not supported: " + packet.toString());



            	//System.out.println(packet.data.toString());
            }
        }
	}
	
}
